--    Program: 		CAPPOIMP_LOCATION_TL.sql
--    Description:  Import PO LOCATION information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         27-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_location_id			number;
                    		                 		
   cursor cur_LOCATION is
      select * from CRST_HR_LOCATIONS_ALL_TL;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_LOCATION loop   	
     l_err_msg_tmp := '';
     
     l_location_id := null;
     SELECT location_id_n
     INTO   l_location_id
     FROM   CRST_HR_LOCATIONS_ALL
     WHERE  location_id = cur.LOCATION_ID;
     
     UPDATE HR_LOCATIONS_ALL_TL
     SET LOCATION_CODE = CAPBRIMP_MAPPING.RenameStr(Substr(LOCATION_CODE,5,4),LOCATION_CODE)
     WHERE LOCATION_CODE = CUR.LOCATION_CODE;
     
     begin
       -- import data
       insert into HR_LOCATIONS_ALL_TL
       (
         LOCATION_ID        ,
         LANGUAGE           ,
         SOURCE_LANG        ,
         LOCATION_CODE      ,
         DESCRIPTION        ,
         LAST_UPDATE_DATE   ,
         LAST_UPDATED_BY    ,
         LAST_UPDATE_LOGIN  ,
         CREATED_BY         ,
         CREATION_DATE
       )
       values
       (
         l_location_id                  ,
         cur.LANGUAGE           		,
         cur.SOURCE_LANG        		,
         cur.LOCATION_CODE      		,
         cur.DESCRIPTION        		,
         cur.LAST_UPDATE_DATE   		,
         CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
         cur.LAST_UPDATE_LOGIN  		,
         CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
         cur.CREATION_DATE
       );  
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOIMP_LOCATION_TL', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
